package com.Guo.auth;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author Admin
 * @project JSONAuthentication
 * @description   封装输出json格式的类
 * @date 2020-12-15-14:50
 */
public abstract class JSONAuthentication {

    /**
     * 输出json
     * @param response
     * @param request
     * @param data
     * @throws IOException
     * @throws ServletException
     */
    protected void WriteJSON(HttpServletResponse response,
                             HttpServletRequest request,
                             Object data) throws IOException, ServletException {
        response.setContentType("application/json;charset=UTF-8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Method", "POST,GET");
        //输出json
        PrintWriter out = response.getWriter();
        //writeValueAsString(data)将value值以json返回
        out.write(new ObjectMapper().writeValueAsString(data));
        out.flush();
        out.close();
    }
}
